<?php
class TagsController extends AppController {

    public $helpers = array('Pagination','Form','Rating');

    public function admin_index() {
        $this->Tag->recursive = 1;
        $this->set('tags', $this->paginate());
    }

    public function admin_add() {
        if (!empty($this->data)) {
            $this->Tag->create();
            if ($this->Tag->save($this->data)) {
                $this->Session->setFlash(__('The Tag has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The Tag could not be saved. Please, try again.',true));
            }
        }
    }

    public function admin_edit($id = null) {
        if (!$this->Tag->findById($id)) {
            $this->Session->setFlash(__('Invalid tag',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        if (!empty($this->data)) {
            if ($this->Tag->save($this->data)) {
                $this->Session->setFlash(__('The Tag has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The Tag could not be saved. Please, try again.',true));
            }
        } else {
            $this->data = $this->Tag->read(null, $id);
        }
    }

    public function admin_delete($id = null) {
        if (!$this->Tag->findById($id)) {
            $this->Session->setFlash(__('Invalid tag',true));
        }
        if ($this->Tag->del($id)) {
            $this->Session->setFlash(__('Tag deleted',true));
        } else {
            $this->Session->setFlash(__('The Tag could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function admin_delete_tags() {
        if($this->Tag->deleteAll(array('Tag.id'=>$this->data['TagsDelete']))) {
            $this->Session->setFlash(__('Tags deleted',true));
        } else {
            $this->Session->setFlash(__('The Tags could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function index() {
        $this->Tag->recursive = 1;
        $this->set('tags', $this->paginate());
    }

    public function view($seo = null) {
        if (!$this->Tag->findBySeo_link($seo)) {
            $this->Session->setFlash(__('Invalid tag',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        $this->Tag->recursive = 2;
        $this->paginate = array('conditions'=>'seo_link="'.$seo.'"','limit'=>5);
        $this->set('tag', $this->paginate());
    }

    public function sidebox() {
        return $this->Tag->find('all');
    }
}
?>